#!/usr/bin  python
# _*_ coding:utf-8 _*_
"""
----------------------------------
    File Name: allergies
    Description:
        Author: lhw
        Date: 2020/9/23
-----------------------------------
"""
from rest_framework.views import APIView
from rest_framework.response import Response
from extensions.auth import JwtAuthorizationAuthentication
from Baby import models
from Baby.serializers import allergies
from Common.logger import app_log
from Common.response import resp


class AllergiesView(APIView):
    # authentication_classes = [JwtAuthorizationAuthentication, ]

    def get(self, request, *args, **kwargs):
        try:
            allergies_query = models.Allergies.objects.all()
            allergies_data = allergies.AllergiesModelSerializer(allergies_query, many=True).data
            return Response({
                "error_code": 0,
                "message": "Success",
                "data": allergies_data
            })
        except Exception as e:
            app_log.exception(e)
            return resp(-1, e.args)

    def post(self, request, *args, **kwargs):
        try:
            request_data = request.data
            allergies_ser = allergies.AllergiesModelSerializer(data=request_data)
            allergies_ser.is_valid(raise_exception=True)
            allergies_obj = allergies_ser.save()
            return Response({
                "error_code": 0,
                "message": "Success",
                "data": allergies.AllergiesModelSerializer(allergies_obj).data
            })
        except Exception as e:
            app_log.exception(e)
            return resp(-1, e.args)

    def put(self, request, *args, **kwargs):
        try:
            request_data = request.data
            allergies_id = request_data["id"]
            old_allergies_obj = models.Allergies.objects.filter(id=allergies_id).first()
            allergies_ser = allergies.AllergiesModelSerializer(instance=old_allergies_obj,
                                                               data=request_data, partial=True)
            allergies_ser.is_valid(raise_exception=True)
            allergies_obj = allergies_ser.save()
            return Response({
                "error_code": 0,
                "message": "Success",
                "data": allergies.AllergiesModelSerializer(allergies_obj).data
            })
        except Exception as e:
            app_log.exception(e)
            return resp(-1, e.args)

    def delete(self, request, *args, **kwargs):
        try:
            allergies_id = request.GET.get("id")
            allergies_data = models.Allergies.objects.filter(id=allergies_id).first()
            if allergies_data:
                models.Allergies.objects.filter(id=allergies_id).delete()
                return Response({
                    "error_code": 0,
                    "message": "Success"
                })
            else:
                return resp(2005, {})
        except Exception as e:
            app_log.exception(e)
            return resp(-1, e.args)
